package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.artisan.dao.TeacherDao;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ManageTeacherFrm extends JInternalFrame {
	private JTable teacherListTable;
	private JTextField searchTeacherNameTextField;
	private JTextField editTeacherNameTextField;
	private JTextField editTeacherTitleTextField;
	private JTextField editTeacherAgeTextField;
	private JPasswordField editTeacherPasswordField;
	private JButton searchTeacherButton;
	private JRadioButton editTeacherSexManRadioButton;
	private JRadioButton editTeacherSexFemaleRadioButton;
	private JButton deleteTeacherButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ManageTeacherFrm frame = new ManageTeacherFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ManageTeacherFrm() {
		setFrameIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/老师.png")));
		setClosable(true);
		setIconifiable(true);

		setTitle("教师信息管理");
		setBounds(100, 100, 920, 612);

		JScrollPane scrollPane = new JScrollPane();

		JLabel lblNewLabel = new JLabel("教师姓名:");
		lblNewLabel.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/老师.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		searchTeacherNameTextField = new JTextField();
		searchTeacherNameTextField.setColumns(10);

		searchTeacherButton = new JButton("查询");
		searchTeacherButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchTeacher(e);
			}
		});
		searchTeacherButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/搜索.png")));
		searchTeacherButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(
				new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)),
				"\u6559\u5E08\u4FE1\u606F\u4FEE\u6539", TitledBorder.LEADING, TitledBorder.TOP, null,
				new Color(0, 0, 0)));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(248).addComponent(lblNewLabel).addGap(18)
								.addComponent(searchTeacherNameTextField, GroupLayout.PREFERRED_SIZE, 178,
										GroupLayout.PREFERRED_SIZE)
								.addGap(54).addComponent(searchTeacherButton, GroupLayout.PREFERRED_SIZE, 97,
										GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup().addGap(118)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(panel, GroupLayout.PREFERRED_SIZE, 682,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 672, Short.MAX_VALUE))))
				.addGap(108)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(42)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(searchTeacherButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(searchTeacherNameTextField, GroupLayout.DEFAULT_SIZE, 33,
												Short.MAX_VALUE)
										.addComponent(lblNewLabel)))
						.addGap(33)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 271, GroupLayout.PREFERRED_SIZE)
						.addGap(18).addComponent(panel, GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
						.addContainerGap()));

		JLabel lblNewLabel_1 = new JLabel("教师姓名:");
		lblNewLabel_1.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/老师.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editTeacherNameTextField = new JTextField();
		editTeacherNameTextField.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("教师性别:");
		lblNewLabel_2.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/性别.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		ButtonGroup buttonGroup = new ButtonGroup();

		editTeacherSexManRadioButton = new JRadioButton("男");
		editTeacherSexManRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editTeacherSexFemaleRadioButton = new JRadioButton("女");
		editTeacherSexFemaleRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		buttonGroup.add(editTeacherSexManRadioButton);
		buttonGroup.add(editTeacherSexFemaleRadioButton);

		JLabel lblNewLabel_3 = new JLabel("教师职称:");
		lblNewLabel_3.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/职称评定.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editTeacherTitleTextField = new JTextField();
		editTeacherTitleTextField.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("教师年龄:");
		lblNewLabel_4.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/年龄.png")));
		lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editTeacherAgeTextField = new JTextField();
		editTeacherAgeTextField.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("登陆密码:");
		lblNewLabel_5.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/密码.png")));
		lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editTeacherPasswordField = new JPasswordField();

		JButton editTeacherSubmitButton = new JButton("确认修改");
		editTeacherSubmitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				editTeacherAct(ae);
			}
		});
		editTeacherSubmitButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/确认.png")));
		editTeacherSubmitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		deleteTeacherButton = new JButton("删除");
		deleteTeacherButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				deleteTeacher(ae);
			}
		});
		deleteTeacherButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		deleteTeacherButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/com/artisan/image/删除.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(lblNewLabel_5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblNewLabel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblNewLabel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				.addPreferredGap(ComponentPlacement.UNRELATED)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(editTeacherPasswordField, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
						.addComponent(editTeacherTitleTextField, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
						.addComponent(
								editTeacherNameTextField, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE))
				.addGap(97)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel.createSequentialGroup()
						.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(editTeacherSexManRadioButton, GroupLayout.PREFERRED_SIZE, 48,
								GroupLayout.PREFERRED_SIZE)
						.addGap(18).addComponent(editTeacherSexFemaleRadioButton))
						.addGroup(gl_panel.createSequentialGroup().addComponent(editTeacherSubmitButton)
								.addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
								.addComponent(deleteTeacherButton, GroupLayout.PREFERRED_SIZE, 112,
										GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblNewLabel_4, GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.UNRELATED).addComponent(editTeacherAgeTextField,
										GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)))
				.addGap(150)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
						.addComponent(editTeacherNameTextField, GroupLayout.PREFERRED_SIZE, 32,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2).addComponent(editTeacherSexManRadioButton)
						.addComponent(editTeacherSexFemaleRadioButton))
				.addGap(18)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_3)
						.addComponent(editTeacherTitleTextField, GroupLayout.PREFERRED_SIZE, 34,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(editTeacherAgeTextField, GroupLayout.PREFERRED_SIZE, 33,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_4))
				.addGap(18)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_5)
						.addComponent(editTeacherPasswordField, GroupLayout.PREFERRED_SIZE, 31,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(editTeacherSubmitButton).addComponent(deleteTeacherButton))
				.addContainerGap(10, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);

		teacherListTable = new JTable();
		teacherListTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				selectedTableRow(e);
			}
		});
		teacherListTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		teacherListTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u6559\u5E08ID", "\u6559\u5E08\u59D3\u540D", "\u6559\u5E08\u6027\u522B",
						"\u6559\u5E08\u804C\u79F0", "\u6559\u5E08\u5E74\u9F84", "\u767B\u9646\u5BC6\u7801" }) {
			boolean[] columnEditables = new boolean[] { false, false, false, false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(teacherListTable);
		getContentPane().setLayout(groupLayout);
		setTable(new Teacher());
		setAuthority();
	}

	protected void editTeacherAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = teacherListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选择要求改的数据!");
			return;
		}
		String teacherName = editTeacherNameTextField.getText().toString();
		String teacherSex = editTeacherSexManRadioButton.isSelected()
				? editTeacherSexManRadioButton.getText().toString()
				: editTeacherSexFemaleRadioButton.getText().toString();
		String teacherTitle = editTeacherTitleTextField.getText().toString();
		int teacherAge = 0;
		try {
			teacherAge = Integer.parseInt(editTeacherAgeTextField.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(this, "年龄只允许输入数字!");
			return;
		}
		String teacherPassword = String.valueOf(editTeacherPasswordField.getPassword());
		if (StringUtil.isEmpty(teacherName)) {
			JOptionPane.showMessageDialog(this, "教师姓名必须填写!");
			return;
		}
		if (StringUtil.isEmpty(teacherTitle)) {
			JOptionPane.showMessageDialog(this, "教师职称必须填写!");
			return;
		}
		if (teacherAge <= 0) {
			JOptionPane.showMessageDialog(this, "教师年龄必须大于0!");
			return;
		}
		if (StringUtil.isEmpty(teacherPassword)) {
			JOptionPane.showMessageDialog(this, "教师登录密码必须填写!");
			return;
		}
		Teacher teacher = new Teacher();
		teacher.setId(Integer.parseInt(teacherListTable.getValueAt(row, 0).toString()));
		teacher.setName(teacherName);
		teacher.setSex(teacherSex);
		teacher.setTitle(teacherTitle);
		teacher.setAge(teacherAge);
		teacher.setPassword(teacherPassword);
		TeacherDao teacherDao = new TeacherDao();
		if (teacherDao.update(teacher)) {
			JOptionPane.showMessageDialog(this, "修改成功!");
		} else {
			JOptionPane.showMessageDialog(this, "修改失败!");
		}
		teacherDao.closeDao();
		setTable(new Teacher());
	}

	protected void searchTeacher(ActionEvent e) {
		// TODO 自动生成的方法存根
		String teacherNameString = searchTeacherNameTextField.getText().toString();
		Teacher teacher = new Teacher();
		teacher.setName(teacherNameString);
		setTable(teacher);
	}

	protected void deleteTeacher(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = teacherListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选择要删除的数据!");
			return;
		}
		if (JOptionPane.showConfirmDialog(this, "确定要删除吗?", "删除提示", 0) != JOptionPane.OK_OPTION) {
			return;
		}
		int id = Integer.parseInt(teacherListTable.getValueAt(row, 0).toString());
		TeacherDao teacherDao = new TeacherDao();
		if (teacherDao.delete(id)) {
			JOptionPane.showMessageDialog(this, "删除成功!");
		} else {
			JOptionPane.showMessageDialog(this, "删除失败!");
		}
		teacherDao.closeDao();
		setTable(new Teacher());
	}

	private void setTable(Teacher teacher) {
		if ("教师".equals(MainFrm.userType.getName())) {
			Teacher tLogined = (Teacher) MainFrm.userObject;
			teacher.setName(tLogined.getName());
			searchTeacherNameTextField.setText(teacher.getName());
		}
		DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
		dft.setRowCount(0);
		TeacherDao teacherDao = new TeacherDao();
		List<Teacher> teacherList = teacherDao.getTeacherList(teacher);
		for (Teacher t : teacherList) {
			Vector v = new Vector();
			v.add(t.getId());
			v.add(t.getName());
			v.add(t.getSex());
			v.add(t.getTitle());
			v.add(t.getAge());
			v.add(t.getPassword());
			dft.addRow(v);
		}
		teacherDao.closeDao();
	}

	protected void selectedTableRow(MouseEvent me) {
		// TODO 自动生成的方法存根
		DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
		editTeacherNameTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 1).toString());
		editTeacherTitleTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 3).toString());
		editTeacherAgeTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 4).toString());
		editTeacherPasswordField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 5).toString());
		String sex = dft.getValueAt(teacherListTable.getSelectedRow(), 2).toString();
		if (sex.equals(editTeacherSexManRadioButton.getText())) {
			editTeacherSexManRadioButton.setSelected(true);
		}
		if (sex.equals(editTeacherSexFemaleRadioButton.getText())) {
			editTeacherSexFemaleRadioButton.setSelected(true);
		}
	}

	private void setAuthority() {
		if ("教师".equals(MainFrm.userType.getName())) {
			deleteTeacherButton.setEnabled(false);
			searchTeacherNameTextField.setEnabled(false);
			editTeacherTitleTextField.setEnabled(false);
		}
	}
}
